/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-20     great_master       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <stdbool.h>
#include "humidity.h"

#define ADC_DEV_NAME    "adc0"
#define ADC_DEV_CHANNEL 7 // PE.5
#define REFER_VOLTAGE   3.3 /* 参考电压 3.3V,数据精度乘以 100 保留 2 位小数*/
#define CONVERT_BITS    (1 << 10) /* 转换位数为 10 位 */

rt_adc_device_t adc_dev =  RT_NULL;
//static int adc_vol_sample(float *vol);


vol_humidity_tab_t vol_humidity_table[] =
{
    {.vol = 0.0, .humidity = 0},
    {.vol = 0.1, .humidity = 0},
    {.vol = 0.2, .humidity = 0},
    {.vol = 0.3, .humidity = 0},
    {.vol = 0.4, .humidity = 0},
    {.vol = 0.5, .humidity = 0},
    {.vol = 0.6, .humidity = 0},
    {.vol = 0.7, .humidity = 0},
    {.vol = 0.8, .humidity = 0},
    {.vol = 0.9, .humidity = 0},
    {.vol = 1.0, .humidity = 0},
    {.vol = 1.1, .humidity = 0},
    {.vol = 1.2, .humidity = 0},
    {.vol = 1.3, .humidity = 0},
    {.vol = 1.4, .humidity = 0},
    {.vol = 1.5, .humidity = 0},
    {.vol = 1.6, .humidity = 0},
    {.vol = 1.7, .humidity = 0},
    {.vol = 1.8, .humidity = 0},
    {.vol = 1.9, .humidity = 0},
    {.vol = 2.0, .humidity = 0},
    {.vol = 2.1, .humidity = 0},
    {.vol = 2.2, .humidity = 0},
    {.vol = 2.3, .humidity = 0},
    {.vol = 2.4, .humidity = 0},
    {.vol = 2.5, .humidity = 0},
    {.vol = 2.6, .humidity = 0},
    {.vol = 2.7, .humidity = 0},
    {.vol = 2.8, .humidity = 0},
    {.vol = 2.9, .humidity = 0},
    {.vol = 3.0, .humidity = 0},
    {.vol = 3.1, .humidity = 0},
    {.vol = 3.2, .humidity = 0},
    {.vol = 3.3, .humidity = 0},
};

void get_current_humidity(uint8_t *humidity)
{
    float vol = 0.0f;

    adc_vol_sample(&vol);
    rt_kprintf("vol:" LOG_FLOAT_MARKER "V \r\n", LOG_FLOAT_VOL(vol));
#if 0
    for(uint8_t i = 0; i < sizeof(vol_humidity_table); i++)
    {
        if((vol_humidity_table[i].vol >= vol) && (i != 0))
        {
            float ratio = (vol - vol_humidity_table[i - 1].vol) /
                          (vol_humidity_table[i].vol - vol_humidity_table[i - 1].vol);
            *humidity = (vol_humidity_table[i].humidity - vol_humidity_table[i - 1].humidity) * ratio +
                        vol_humidity_table[i - 1].humidity;
        }
    }
    *humidity = 0; // indicate error value
#endif
}

int adc_vol_sample(float *vol)
{
    rt_uint32_t value;

    if(adc_dev  == RT_NULL)
        return RT_ERROR;
    value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
    //rt_kprintf("the value is :%d \n", value);
    *vol = value * REFER_VOLTAGE / CONVERT_BITS;

    return RT_EOK;
}
MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample)

int humidity_init(void)
{
    rt_uint32_t vol;
    rt_err_t ret = RT_EOK;

    adc_dev = (rt_adc_device_t) rt_device_find(ADC_DEV_NAME);
    if (adc_dev == RT_NULL)
    {
        rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
        return RT_ERROR;
    }
    ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);

    return ret;
}

